export type Feature = {
  type: string;
  color: string;
  properties: {
    handle: string;
    ownerHandle: string;
    layer: string;
    lineType: string;
    colorIndex: number;
    lineTypeScale: number;
    lineWeight: number;
    stroke: string;
  };
  geometry: {
    type: string;
    coordinates: number[][] | number[];
  };
};

export function generateColorFromText(text) {
  // 将文本转换为十六进制数字
  let hashCode = 0;
  for (let i = 0; i < text.length; i++) {
    hashCode = text.charCodeAt(i) + ((hashCode << 5) - hashCode);
  }

  // 根据哈希码生成颜色
  let color = '#';
  for (let j = 0; j < 3; j++) {
    const value = (hashCode >> (j * 4)) & 0xff;
    color += value.toString(16).padStart(2, '0');
  }

  return color;
}

export function getFeatureGroups(json: any) {
  const group: { [key: string]: Feature[] } = {};

  json.features.forEach((item) => {
    if (!item.properties.layer) {
      item.properties.layer = 'default'
    }
    if (item.properties.layer in group === false) {
      group[item.properties.layer] = [];
    }

    group[item.properties.layer].push(item as Feature);
  });

  return group;
}
